﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.ClassFactory;
using BusinessLogic.BusinessRules;
using Common;
using IssueCommon;



namespace RichECMS.ServerPages
{
    public partial class Left : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Convert.ToInt32(State.intSTID) != -1)
                {
                    InitDataBind(State.intSTID);
                    hiddenpid.Value = State.intSTID.ToString();
                    ALookWeb.HRef = "/news/" + State.LoginUser.VartualPath + "/" + IssueBase.strDefaultFileName + AppConfig.GetWebFileType();
                    if (State.LoginUser.ManagerCompetence <= (int)Utility.ManagerCompetence.Designer)
                    {
                        ASectionSet.Visible = true;
                        ATemplateSet.Visible = true;
                        APageEditor.Visible = true;
                        AmanagerClients.Visible = true;
                        AAccountManager.Visible = true;
                        AWebSetting.Visible = true;
                    }
                    else if (State.LoginUser.ManagerCompetence == (int)Utility.ManagerCompetence.Admin)
                    {
                        ASectionSet.Visible = true;
                        ATemplateSet.Visible = true;
                        APageEditor.Visible = false;
                        AmanagerClients.Visible = true;
                        AAccountManager.Visible = true;
                        AWebSetting.Visible = false;
                    }
                    else if (State.LoginUser.ManagerCompetence == (int)Utility.ManagerCompetence.UserCanIssue)
                    {
                        ASectionSet.Visible = false;
                        ATemplateSet.Visible = false;
                        APageEditor.Visible = false;
                        AmanagerClients.Visible = false;
                        AAccountManager.Visible = false;
                        AWebSetting.Visible = false;
                    }
                    else if (State.LoginUser.ManagerCompetence == (int)Utility.ManagerCompetence.UserCanIssueAndClients)
                    {
                        ASectionSet.Visible = true;
                        ATemplateSet.Visible = false;
                        APageEditor.Visible = false;
                        AmanagerClients.Visible = true;
                        AAccountManager.Visible = false;
                        AWebSetting.Visible = false;
                    }
                    else if (State.LoginUser.ManagerCompetence >= (int)Utility.ManagerCompetence.User)
                    {
                        ASectionSet.Visible = false;
                        ATemplateSet.Visible = false;
                        APageEditor.Visible = false;
                        AmanagerClients.Visible = false;
                        AAccountManager.Visible = false;
                        AWebSetting.Visible = false;
                    }
                }
            }
        }

        /// <summary>
        /// 绑定
        /// </summary>
        private void InitDataBind(int intParentSectionId)
        {
            try
            {
                using (SectionsFactory objFactory = new SectionsFactory())
                {
                    BusinessLogic.BusinessRules.Sections objsection = objFactory.NewInstance();
                    DataSet dstSectionList = new DataSet();
                    dstSectionList.Tables.Add(objsection.Data.Sections.DataSet.Tables[0].Clone());
                    //添加本板块
                    objsection = objFactory.CreatorBySql(" intSectionID =" + intParentSectionId.ToString() + " and intWSID=" + State.intWSID);
                    dstSectionList.Tables[0].Rows.Add(objsection.Data.Sections.DataSet.Tables[0].Rows[0].ItemArray);
                    //轮询子版块
                    string strfilter = "";
                    if (State.LoginUser.ManagerCompetence > 3)
                    {
                        if (State.LoginUser.Permissions != "")
                        {
                            strfilter = " and intSectionID in (" + State.LoginUser.Permissions + ") ";
                            objsection = objFactory.CreatorBySql("intParentID =0 and intWSID=" + State.intWSID + strfilter + "  order by intSortNo desc,  dtmInTime desc ");
                        }
                        else
                        {
                            strfilter = " and intSectionID = -100";
                            Alert("您没有可管理的栏目，请联系网站管理员");
                        }
                    }

                    GetSectionFromParentId(ref dstSectionList, intParentSectionId, strfilter);
                    rptList.DataSource = dstSectionList.Tables[0].DefaultView;
                    rptList.DataBind();
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString());
            }
        }

        /// <summary>
        /// 根据父ID获取子栏目  
        /// </summary>
        private void GetSectionFromParentId(ref DataSet dstSectionList, int intParentId, string strFilter)
        {
            try
            {
                using (SectionsFactory objFactory = new SectionsFactory())
                {
                    BusinessLogic.BusinessRules.Sections objsection = objFactory.CreatorBySql("intParentID =" + intParentId.ToString() + strFilter + " and intWSID=" + State.intWSID + " order by intSortNo desc,dtmInTime desc");
                    if (objsection != null)
                    {
                        BusinessLogic.BusinessRules.Sections objsectionTemp = objFactory.NewInstance(); ;
                        for (int i = 0; i < objsection.Count; i++)
                        {
                            dstSectionList.Tables[0].Rows.Add(objsection.Data.Sections.DataSet.Tables[0].Rows[i].ItemArray);
                            objsectionTemp = objFactory.CreatorBySql("intParentID =" + objsection.DataItem.intSectionID.ToString() + strFilter + " and intWSID=" + State.intWSID + " order by intSortNo desc,dtmInTime desc");
                            if (objsectionTemp != null)
                            {
                                for (int j = 0; j < objsectionTemp.Count; j++)
                                {
                                    dstSectionList.Tables[0].Rows.Add(objsectionTemp.Data.Sections.DataSet.Tables[0].Rows[j].ItemArray);
                                    GetSectionFromParentId(ref dstSectionList, objsectionTemp.DataItem.intSectionID, strFilter);
                                    objsectionTemp.SelectRow(j + 1);
                                }
                            }
                            objsection.SelectRow(i + 1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }
    }
}